/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-09-10
 * 19:27
 */
// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void put(int key, int value) {
        // write code here
        int i=key/array.length;
        Node cur1=array[i];
        while(cur1!=null){
            if(cur1.key==key){
                cur1.value=value;
                return ;
            }
            cur1=cur1.next;
        }

        Node cur=new Node(key,value);
        cur.next=array[i];
        array[i]=cur;
        size++;
        if(loadFactor()>LOAD_FACTOR){
            resize();

        }

    }


    private void resize() {
        // write code here
        Node[] newArray=new Node[array.length*2];//扩容2倍
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while (cur!=null){
                int newIndex= cur.key/newArray.length;
                Node curN=cur.next;
                newArray[newIndex]=cur;
                cur=newArray[newIndex];
                cur=curN;
            }
        }
        array=newArray;
    }


    private double loadFactor() {

        return size * 1.0 / array.length;
    }


    public HashBucket() {
        // write code here
    }


    public int get(int key) {
        // write code here
        return 0;
    }
}